
#include <sstream>
using namespace std;
#include "ScriptRunner.h"
#include "../Node/KmyNodeImage.h"
#include "../KmyAlgorithm.h"
#include "../KmyIControllerPad.h"
#include "KmyVarInt.h"
#include "KmyVarFloat.h"
#include "KmyVarString.h"

ScriptRunner::ScriptRunner()
{
    curscript = NULL;
    macro_dict = NULL;

    cross_up_func = NULL;
    cross_down_func = NULL;
    cross_left_func = NULL;
    cross_right_func = NULL;

    button_A_func = NULL;
    button_B_func = NULL;
    button_X_func = NULL;
    button_Y_func = NULL;
    button_SELECT_func = NULL;
    button_START_func = NULL;
    button_L_func = NULL;
    button_R_func = NULL;

    debuger = KmyIDebug::getInstance();
}

ScriptRunner::~ScriptRunner(){

}

/**
  * 运行初始化脚本
  * 1 安装事件函数
  * 初始化原始指令表
  */
void ScriptRunner::initialize(KmySceneManager *smr, KmyScript *start_scp, KmyScript *macro_dict){
    curscript = start_scp;
    IP = curscript->getAnchorAddr("*start");
    RP = 0;
    DP = 0;
    wait_time = 0;
    scene_manager = smr;
    this->macro_dict = macro_dict;

    string funcname;
//    void (ScriptRunner::*pfunc)();
    classFunc pfunc;
//    funcname = "store";
//    pfunc = &ScriptRunner::store;
//    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));
//    funcname = "fetch";
//    pfunc = &ScriptRunner::fetch;
//    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));
    funcname = "eval";
    pfunc = &ScriptRunner::eval;
    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));

    funcname = "clearsysvar";
    pfunc = &ScriptRunner::clearsysvar;
    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));
    funcname = "close";
    pfunc = &ScriptRunner::close;
    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));
//    funcname = "cursor";
//    pfunc = &ScriptRunner::cursor;
//    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));
    funcname = "hidemessage";
    pfunc = &ScriptRunner::hidemessage;
    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));
    funcname = "nextskip";
    pfunc = &ScriptRunner::nextskip;
    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));
    funcname = "quake";
    pfunc = &ScriptRunner::quake;
    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));
    funcname = "stopquake";
    pfunc = &ScriptRunner::stopquake;
    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));
    funcname = "s";
    pfunc = &ScriptRunner::stopscript;
    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));
//    funcname = "title";
//    pfunc = &ScriptRunner::title;
//    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));
    funcname = "wait";
    pfunc = &ScriptRunner::WAIT;
    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));
    funcname = "waitclick";
    pfunc = &ScriptRunner::waitclick;
    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));
    funcname = "wc";
    pfunc = &ScriptRunner::waitmessage;
    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));
    funcname = "wq";
    pfunc = &ScriptRunner::waitquake;
    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));

    funcname = "animstart";
    pfunc = &ScriptRunner::animstart;
    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));
    funcname = "animstop";
    pfunc = &ScriptRunner::animstop;
    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));
    funcname = "wa";
    pfunc = &ScriptRunner::waitanim;
    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));
    funcname = "backlay";
    pfunc = &ScriptRunner::backlay;
    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));
//    funcname = "copylay";
//    pfunc = &ScriptRunner::copylay;
//    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));
    funcname = "freeimage";
    pfunc = &ScriptRunner::freeimage;
    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));
    funcname = "image";
    pfunc = &ScriptRunner::image;
    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));
    funcname = "move";
    pfunc = &ScriptRunner::move;
    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));
    funcname = "stopmove";
    pfunc = &ScriptRunner::stopmove;
    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));
    funcname = "wm";
    pfunc = &ScriptRunner::waitmove;
    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));
    funcname = "trans";
    pfunc = &ScriptRunner::trans;
    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));
    funcname = "stoptrans";
    pfunc = &ScriptRunner::stoptrans;
    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));
    funcname = "wt";
    pfunc = &ScriptRunner::waittrans;
    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));

    funcname = "call";
    pfunc = &ScriptRunner::CALL;
    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));
    funcname = "link";
    pfunc = &ScriptRunner::LINK;
    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));
    funcname = "endlink";
    pfunc = &ScriptRunner::endlink;
    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));
    funcname = "jump";
    pfunc = &ScriptRunner::JUMP;
    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));
    funcname = "return";
    pfunc = &ScriptRunner::RETURN;
    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));

    funcname = "message";
    pfunc = &ScriptRunner::message;
    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));
    funcname = "phonetic";
    pfunc = &ScriptRunner::phonetic;
    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));
    funcname = "name";
    pfunc = &ScriptRunner::NAME;
    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));
    funcname = "ruby";
    pfunc = &ScriptRunner::ruby2;
    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));
    funcname = "/ruby";
    pfunc = &ScriptRunner::endruby;
    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));
    funcname = "cm";
    pfunc = &ScriptRunner::clearmessage;
    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));
    funcname = "l";
    pfunc = &ScriptRunner::linewait;
    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));
    funcname = "p";
    pfunc = &ScriptRunner::pagewait;
    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));
    funcname = "er";
    pfunc = &ScriptRunner::clearcurmsg;
    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));
    funcname = "delay";
    pfunc = &ScriptRunner::setdelay;
    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));
    funcname = "cancelautomode";
    pfunc = &ScriptRunner::cancelautomode;
    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));
    funcname = "r";
    pfunc = &ScriptRunner::endline;
    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));
    funcname = "cancelskip";
    pfunc = &ScriptRunner::cancelskip;
    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));

    funcname = "addsprite";
    pfunc = &ScriptRunner::addSprite;
    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));
    funcname = "removesprite";
    pfunc = &ScriptRunner::removeSprite;
    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));
    funcname = "updatesprite";
    pfunc = &ScriptRunner::updateSprite;
    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));
    funcname = "joinmode";
    pfunc = &ScriptRunner::joinmode;
    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));
    funcname = "leavemode";
    pfunc = &ScriptRunner::leavemode;
    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));
    funcname = "targetscreen";
    pfunc = &ScriptRunner::targetscreen;
    orig_inst_map.insert(pair<string, classFunc>(funcname, pfunc));
}

void ScriptRunner::finalize(){

    // RStack
    // DStack
    // sys_var
    // systimer
    // img_btns
    // curscript
}

void ScriptRunner::logic(){
    if (curscript==NULL){
        return;
    }
    if (control_enable){
        KmyIControllerPad *cp = scene_manager->getControllerPad();
        if (crossbutton_enable){
            if (cross_up_func!=NULL && cp->isKeyDown(KmyIControllerPad::KEY_UP)){
                call(cross_up_func);
            }else if (cross_down_func!=NULL && cp->isKeyDown(KmyIControllerPad::KEY_DOWN)){
                call(cross_down_func);
            }
            if (cross_left_func!=NULL && cp->isKeyDown(KmyIControllerPad::KEY_LEFT)){
                call(cross_left_func);
            }else if (cross_right_func!=NULL && cp->isKeyDown(KmyIControllerPad::KEY_RIGHT)){
                call(cross_right_func);
            }
        }
        if (button_A_func!=NULL && cp->isKeyPress(KmyIControllerPad::KEY_A)){
//            call(button_A_func);
            next();
        }
        if (button_B_func!=NULL && cp->isKeyPress(KmyIControllerPad::KEY_B)){
            call(button_B_func);
        }
        if (button_X_func!=NULL && cp->isKeyPress(KmyIControllerPad::KEY_X)){
            call(button_X_func);
        }
        if (button_Y_func!=NULL && cp->isKeyPress(KmyIControllerPad::KEY_Y)){
            call(button_Y_func);
        }
    }

    // 当要wait时 return
    int cur_time = scene_manager->getEnginePassTime();
    if (cur_time<wait_time){
        return;
    }else{
        wait_time = 0;
    }
    if (!is_pause){
        run();
    }
}

void ScriptRunner::run(){
    is_return = false;
    // 不需要wait时运行脚本，碰到wait指令时强行退出
    string inst = curscript->getInstruction(IP);
    while (!inst.empty()){
        IP++;

        int result = execute(inst);
        if (is_return){
            return;
        }
        if (result==-1){

        }
        /** 如果需要等待则退出 */
        if (wait_time>scene_manager->getEnginePassTime()){
            return;
        }
        inst = curscript->getInstruction(IP);
    }
}

void ScriptRunner::next(){
    switch(next_type){
    case 1:     // 文字加速显示
        cur_textlayer->immediateOver();
        next_type = 2;
        break;
    case 2:     // 下一句
        wait_time = 0;
        is_pause = false;
        break;
    case 3:     // 换页
        cur_textlayer->clear();
        wait_time = 0;
        is_pause = false;
        break;
    }
}

int ScriptRunner::execute(const string &instruction){
    const char* t = instruction.c_str();
    if (t[0]=='_'){
        string name(&t[1]);
        KmyVar * v2 = DStack.top();

        DStack.pop();
        addParam(name, v2);
        return EXECULT_RESULT_PARAM;
    }
    classFunc oi = getOrigInst(instruction);
    if (oi!=NULL){
        // 执行原始指令
        (this->*oi)();
        return EXECUTE_RESULT_ORIG;
    }else{
        if (macro_dict!=NULL){
            int macro = macro_dict->getAnchorAddr(instruction);
            if (macro!=-1){
                callMacro(macro);
                return EXECUTE_RESULT_MACRO;
            }else{
                //进栈
                DStack.push(KmyVar::createVar(instruction));
                return EXECUTE_RESULT_STACK;
            }
        }else{
            //进栈
            DStack.push(KmyVar::createVar(instruction));
            return EXECUTE_RESULT_STACK;
        }
    }
}

/**
  * 跳转脚本中的标签（可返回）
  * 当前位置要保存，以便返回
  */
void ScriptRunner::call(KmyScriptAnchor *ksa){
    KmyScript *script = curscript;
    if (this->curscript->name != ksa->script_name){
        KmyResourceManager* krm = KmyResourceManager::getInstance();
        script = krm->getScript(ksa->script_name);
    }
    if (script==NULL){
        *debuger<<"can't find script:"<<ksa->script_name<<debuger->endl();
        return;
    }

    KmyScriptAnchor *return_script_anchor = new KmyScriptAnchor;
    return_script_anchor->script_name = curscript->name;
    return_script_anchor->anchor_addr = IP;
    RStack.push(return_script_anchor);

    curscript = script;
    if (ksa->anchor_addr==-2){
        ksa->anchor_addr = script->getAnchorAddr(ksa->anchor_name);
    }
    if (ksa->anchor_addr!=-1){
        IP = ksa->anchor_addr;
    }else{
        *debuger<<"can't find anchor:"<<ksa->script_name<<"."<<ksa->anchor_name<<debuger->endl();
        return;
    }
    run();
}

/**
  * 跳转脚本中的标签（不可返回）
  */
void ScriptRunner::jump(KmyScriptAnchor* ksa){
    KmyScript *script = curscript;
    if (this->curscript->name != ksa->script_name){
        KmyResourceManager* krm = KmyResourceManager::getInstance();
        script = krm->getScript(ksa->script_name);
    }
    if (script==NULL){
        *debuger<<"can't find script:"<<ksa->script_name<<debuger->endl();
        return;
    }

    curscript = script;
    if (ksa->anchor_addr==-2){
        ksa->anchor_addr = script->getAnchorAddr(ksa->anchor_name);
    }
    if (ksa->anchor_addr!=-1){
        IP = ksa->anchor_addr;
    }else{
        *debuger<<"can't find anchor:"<<ksa->script_name<<"."<<ksa->anchor_name<<debuger->endl();
        return;
    }
    run();
}

/**
  * 调用宏指令(带返回）
  * 如果param_list不空，则全部转到macro_param_list，清空param_list
  * 若一般指令的参数取出来为"%"开头，则再从macro_param_list里去参数。
  */
void ScriptRunner::callMacro(int macro){
    if (param_list.empty()){
        map<string, KmyVar*>::iterator iter = param_list.begin();
        for (; iter != param_list.end(); iter++){
            string key = iter->first;
            KmyVar *p=iter->second;
            addMacroParam(key, p);
        }
        param_list.clear();
    }
    KmyScriptAnchor *msa = new KmyScriptAnchor();
    msa->script_name = macro_dict->name;
    msa->anchor_addr = macro;
    call(msa);
    delete msa;
}

void ScriptRunner::endMacro(){
    clearMacroParamList();
    RETURN();
}

void ScriptRunner::wait(int w_time){
    int cur_time = scene_manager->getEnginePassTime();

    wait_time = cur_time + w_time;
}


void ScriptRunner::waitCancel(){
    wait_time = 0;
}

classFunc ScriptRunner::getOrigInst(const string &inst_name){
    map<string, classFunc>::iterator iter = orig_inst_map.find(inst_name);
    if (iter!=orig_inst_map.end()){
        return iter->second;
    }else{
        return NULL;
    }
}


//void ScriptRunner::onTouch(int x, int y){

//}

void ScriptRunner::addSystemVar(const string &name, KmyVar *value){
    sys_var.insert(pair<string, KmyVar*>(name, value));
}

KmyVar* ScriptRunner::getSystemVar(const char *name){
    string sname(name);
    map<string, KmyVar*>::iterator iter = sys_var.find(sname);
    if (iter!=sys_var.end()){
        return iter->second;
    }
    return NULL;
}

void ScriptRunner::addParam(string &name, KmyVar *value){
    param_list.insert(pair<string, KmyVar*>(name, value));
}

KmyVar *ScriptRunner::getParam(const char *pname){
    string sname(pname);
    map<string, KmyVar*>::iterator iter = param_list.find(sname);
    if (iter!=param_list.end()){
        return iter->second;
    }
    return NULL;
}

KmyVar *ScriptRunner::removeParam(const char *pname){
    string sname(pname);
    map<string, KmyVar*>::iterator iter = param_list.find(sname);
    if (iter!=param_list.end()){
        KmyVar *v = iter->second;
        param_list.erase(iter);
        return v;
    }
    return NULL;
}

void ScriptRunner::clearParamList(){
    if (param_list.empty()){
        map<string, KmyVar*>::iterator iter = param_list.begin();
        for (; iter != param_list.end(); iter++){
            KmyVar *p=iter->second;
            delete p;
        }
        param_list.clear();
    }
}

int ScriptRunner::getParamInt(const char *pname, int dft_i){
    KmyVar *temp = getParam(pname);
    if (temp!=NULL){
        if (temp->getVarType()==KmyVar::VARIABLE_TYPE_INT){
            return ((KmyVarInt*)temp)->value;
        }
        if (temp->getVarType()==KmyVar::VARIABLE_TYPE_STRING){
            u8 *v1 = ((KmyVarString*)temp)->data;
            if (*v1 == '%'){ // 如果是%开头则为宏参数
                KmyVar* temp2 = getMacroParam((const char*)v1+1);
                if (temp2!=NULL){
                    if (temp2->getVarType() == KmyVar::VARIABLE_TYPE_INT){
                        return ((KmyVarInt*)temp2)->value;
                    }else{
                        *debuger<<"macro param type wrong"<<debuger->endl();
                    }
                }else{
                    *debuger<<"macro param miss"<<debuger->endl();
                }
            }
        }else{
            *debuger<<"param type wrong"<<debuger->endl();
        }
    }else{
        *debuger<<"param miss"<<debuger->endl();
    }
    return dft_i;
}

float ScriptRunner::getParamFloat(const char *pname, float dft_f){
    KmyVar *temp = getParam(pname);
    if (temp!=NULL){
        if (temp->getVarType()==KmyVar::VARIABLE_TYPE_INT){
            return ((KmyVarInt*)temp)->value;
        }else if (temp->getVarType()==KmyVar::VARIABLE_TYPE_FLOAT){
            return ((KmyVarFloat*)temp)->value;
        }
        if (temp->getVarType()==KmyVar::VARIABLE_TYPE_STRING){
            u8 *v1 = ((KmyVarString*)temp)->data;
            if (*v1 == '%'){ // 如果是%开头则为宏参数
                KmyVar* temp2 = getMacroParam((const char*)v1+1);
                if (temp2!=NULL){
                    if (temp2->getVarType() == KmyVar::VARIABLE_TYPE_INT){
                        return ((KmyVarInt*)temp2)->value;
                    }else if (temp2->getVarType() == KmyVar::VARIABLE_TYPE_FLOAT){
                        return ((KmyVarFloat*)temp2)->value;
                    }else{
                        *debuger<<"macro param type wrong"<<debuger->endl();
                    }
                }else{
                    *debuger<<"macro param miss"<<debuger->endl();
                }
            }
        }else{
            *debuger<<"param type wrong"<<debuger->endl();
        }
    }else{
        *debuger<<"param miss"<<debuger->endl();
    }
    return dft_f;
}

u8* ScriptRunner::getParamString(const char *pname){
    KmyVar *temp = getParam(pname);
    if (temp!=NULL){
        if (temp->getVarType()==KmyVar::VARIABLE_TYPE_STRING){
            u8 *v1 = ((KmyVarString*)temp)->data;
            if (*v1 == '%'){// 如果是%开头则为宏参数
                KmyVar* temp2 = getMacroParam((const char*)v1+1);
                if (temp2!=NULL){
                    if (temp2->getVarType() == KmyVar::VARIABLE_TYPE_STRING){
                        return ((KmyVarString*)temp2)->data;
                    }else{
                        *debuger<<"macro param type wrong"<<debuger->endl();
                    }
                }else{
                    *debuger<<"macro param miss"<<debuger->endl();
                }
            }else{
                return v1;
            }
        }else{
            *debuger<<"param type wrong"<<debuger->endl();
        }
    }else{
        *debuger<<"param miss"<<debuger->endl();
    }
    return NULL;
}

void ScriptRunner::addMacroParam(string &name, KmyVar *value){
    macro_param_list.insert(pair<string, KmyVar*>(name, value));
}

KmyVar* ScriptRunner::getMacroParam(const char *pname){
    string sname(pname);
    map<string, KmyVar*>::iterator iter = macro_param_list.find(sname);
    if (iter!=macro_param_list.end()){
        return iter->second;
    }
    return NULL;
}

void ScriptRunner::clearMacroParamList(){
    if (macro_param_list.empty()){
        map<string, KmyVar*>::iterator iter = macro_param_list.begin();
        for (; iter != macro_param_list.end(); iter++){
            KmyVar *p=iter->second;
            delete p;
        }
        macro_param_list.clear();
    }
}

//// DStack中取出变量名和值，做出<变量名， 值>存进Param_list中
//void ScriptRunner::store(){
//    KmyVar *v1 = DStack.top();
//    if (v1->getVarType()!=KmyVar::VARIABLE_TYPE_STRING){
//        // error;
//        return;
//    }
//    string name((const char*)(((KmyVarString*)v1)->data));
//    DStack.pop();

//    KmyVar * v2 = DStack.top();

//    DStack.pop();
//    addParam(name, v2);
//    delete v1;
//}

//// 取出变量的值，存进DStack
//void ScriptRunner::fetch(){
//    KmyVar *v1 = DStack.top();
//    if (v1->getVarType()!=KmyVar::VARIABLE_TYPE_STRING){
//        // error;
//        return;
//    }
//    string name((const char*)(((KmyVarString*)v1)->data));
//    DStack.pop();

//    KmyVar* value = getParam(name.c_str());
//    DStack.push(value);
//    delete v1;
//}

void ScriptRunner::eval(){
    KmyVar *v1 = DStack.top();
    if (v1->getVarType()!=KmyVar::VARIABLE_TYPE_STRING){
        // error;
        return;
    }
    string exp((const char*)(((KmyVarString*)v1)->data));
    DStack.pop();
    // 编译 exp，并运行
}

void ScriptRunner::clearsysvar(){
    if (!sys_var.empty()){
        map<string, KmyVar*>::iterator iter = sys_var.begin();
        for (; iter != sys_var.end(); iter++){
            KmyVar *p=iter->second;
//            p->finalize();
            delete p;
        }
        sys_var.clear();
    }
}

void ScriptRunner::close(){
    scene_manager->exit();
    clearParamList();
}

//void ScriptRunner::cursor(){

//}

void ScriptRunner::hidemessage(){
    KmyNodeLayer* uilayer = scene_manager->getCurScene()->getUILayer(0);
    uilayer->visible = false;
    uilayer = scene_manager->getCurScene()->getUILayer(1);
    if (uilayer!=NULL){
        uilayer->visible = false;
    }
}

void ScriptRunner::nextskip(){

}

void ScriptRunner::quake(){

}

void ScriptRunner::stopquake(){

}

void ScriptRunner::waitquake(){

}

void ScriptRunner::stopscript(){
    IP--;
}


//void ScriptRunner::title(){

//}
/**
  * 等待一定时间
  * time 等待时间 毫秒
  * canskip 是否可以按键鼠标跳过
  */
void ScriptRunner::WAIT(){

    int time = 0;
    bool canskip = true;

    KmyVar *temp = getParam("time");
    if (temp!=NULL && temp->getVarType()==KmyVar::VARIABLE_TYPE_INT){
        time = ((KmyVarInt*)temp)->value;
    }
    delete temp;

    temp = getParam("canskip");
    if (temp!=NULL && temp->getVarType()==KmyVar::VARIABLE_TYPE_STRING){
        string b((const char*)(((KmyVarString*)temp)->data));
        if (b=="false"){
            canskip = false;
        }
        delete temp;
    }
    wait_time = time;
    is_canskip = canskip;

    clearParamList();
}

void ScriptRunner::waitclick(){
    is_pause = true;
}

void ScriptRunner::waitmessage(){
    if (!cur_textlayer->isIdle()){
        IP--;
    }
}

void ScriptRunner::animstart(){

}

void ScriptRunner::animstop(){

}

void ScriptRunner::waitanim(){

}

void ScriptRunner::backlay(){

}

//void ScriptRunner::copylay(){

//}

/**
  * string name
  * int subimage
  * int id 供freeimage使用
  * int x
  * int y
  * int z
  * float alpha
  * int flip
  * float angle
  */
void ScriptRunner::image(){
    u8 *c1 = getParamString("name");
    string name((const char*)c1);

    int subimage = 0;
    int x = 0;
    int y = 0;
    int z = 0;
    float alpha = 1;
    int flip = 0;
    float angle = 0;
    int id = 0;

    subimage = getParamInt("subimage", 0);
    id = getParamInt("id", -1);
    x = getParamInt("x", 0);
    y = getParamInt("y", 0);
    z = getParamInt("z", 0);
    alpha = getParamFloat("alpha", 1.0);
    flip = getParamInt("flip", 0);
    angle = getParamFloat("angle", 0);

    KmyNodeImage* img = new KmyNodeImage(
                id, name, subimage,
                x, y, z,
                alpha, flip, angle);
    scene_manager->getCurScene()->getUILayer(0)->addChild(img);
    clearParamList();
}

/**
 * id 要擦除的图片id
 */
void ScriptRunner::freeimage(){
    int id = getParamInt("id", 0);

    scene_manager->getCurScene()->removeNode(id);
    clearParamList();
}

/**
  * 移动一个物体
  * id
  * time 时间
  * x， y目标点
  * mode 运动方式
  */
void ScriptRunner::move(){

}

/**
  * id
  */
void ScriptRunner::stopmove(){

}

/**
  * id
  */
void ScriptRunner::waitmove(){

}

/**
  * mode 切换方式
  * from 方向
  */
void ScriptRunner::trans(){

}

/**
  * 强制切换完毕
  */
void ScriptRunner::stoptrans(){

}

void ScriptRunner::waittrans(){

}

void ScriptRunner::CALL(){

}

void ScriptRunner::LINK(){

}

void ScriptRunner::endlink(){

}

void ScriptRunner::JUMP(){

}

void ScriptRunner::RETURN(){
    KmyScriptAnchor *return_script_anchor = RStack.top();
    RStack.pop();

    if (curscript->name != return_script_anchor->script_name){
        KmyResourceManager* krm = KmyResourceManager::getInstance();
        curscript = krm->getScript(return_script_anchor->script_name);
        if (curscript==NULL){
            *debuger<<"error"<<debuger->endl();
        }
    }
    IP = return_script_anchor->anchor_addr;
    is_return = true;
    delete return_script_anchor;
}

/**
  * 在当前文字层显示文字
  * 吃掉index, 从text中取到文字显示
  * 如果phn存在，则显示注音文字
  */
void ScriptRunner::message(){
    int index = getParamInt("index", 0);
    if (index < curscript->text.size()){
        u8* text = curscript->text.at(index)->buff;
        if (phn_text!=NULL){
            cur_textlayer->putUtf8Phonetic2(text, phn_text, 0, 16, 8);
            phn_text = NULL;
        }else{
            cur_textlayer->putUtf8String(text, 0, 16);
        }
    }else{
        *debuger<<"text index error"<<debuger->endl();
    }

    clearParamList();
}

/**
  * 注音文字
  * main
  * phn
  */
void ScriptRunner::phonetic(){
    u8* main;
    u8* phn;

    main = getParamString("main");
    phn = getParamString("phn");

    cur_textlayer->putUtf8Phonetic2(main, phn, 0, 16, 8);
    clearParamList();
}

/**
  * name
  * t 直接名字 / role 角色变量
  */
void ScriptRunner::NAME(){
    u8* name = getParamString("t");
    if (name == NULL){
        name = getParamString("role");
    }
    if (name!=NULL){
        // TODO
    }

    clearParamList();
}

///**
//  * 单个注音（不处理越界，超出屏幕等问题)
//  * text
//  */
//void ScriptRunner::ruby(){

//}

/**
  * 多字注音
  * 吃掉参数text,存入临时变量，遇到下一个指令的message，然后调用putUtf8Phonetic2
  * /ruby直接略过
  */
void ScriptRunner::ruby2(){
    phn_text = getParamString("text");
    clearParamList();
}

/**
  * /ruby直接略过
  */
void ScriptRunner::endruby(){
    clearParamList();
}

/**
  * 清除所有消息层文字
  */
void ScriptRunner::clearmessage(){

}

/**
  * 行末等待点击
  */
void ScriptRunner::linewait(){

}

/**
  * 页末等待点击
  */
void ScriptRunner::pagewait(){

}

/**
  * 清除当前消息层
  */
void ScriptRunner::clearcurmsg(){
    cur_textlayer->clear();
}

/**
  * 文字显示速度设定
  *speed: nowait,user,毫秒值
  */
void ScriptRunner::setdelay(){

}

/**
  * 取消自动播放模式
  */
void ScriptRunner::cancelautomode(){
    automode = false;
}

/**
  * 换行
  */
void ScriptRunner::endline(){
    cur_textlayer->putUtf8String((const u8*)("\n"), 0, 1);
}

/**
  * 取消跳过模式
  */
void ScriptRunner::cancelskip(){
    autoskip = false;
}

/**
  * name 精灵名
  * id
  * x
  * y
  * z
  * flip
  * anime 初始哪个动画
  * astate 动画方式
  */
void ScriptRunner::addSprite(){

}

/**
  * id
  */
void ScriptRunner::removeSprite(){

}

/**
  * id
  * flip
  * anime
  * astate
  */
void ScriptRunner::updateSprite(){

}

/**
  * mode    0直接入场 1 淡入淡出 2 左平移入场 3 右平移入场
  * time
  */
void ScriptRunner::joinmode(){
    obj_joinmode = getParamInt("mode", 0);
    obj_jointime = getParamInt("time", 0);
    clearParamList();
}

/**
  * mode    0直接退场 1 淡入淡出 2 左平移退场 3 右平移退场
  * time
  */
void ScriptRunner::leavemode(){
    obj_leavemode = getParamInt("mode", 0);
    obj_leavetime = getParamInt("time", 0);
    clearParamList();
}

/**
  * index 0/1
  */
void ScriptRunner::targetscreen(){
    screen_no = getParamInt("index", 0);
    clearParamList();
}
